package com.lib.common.view.paypwd;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.lib.utils.ConvertUtils;

/**
 * @author FengLing
 * Created on 2017-08-07 17:42
 */
public class PayPwdView extends View {
    private int mW;
    private int mH;
    private Paint mVLPaint = new Paint();
    private Paint mCirclePaint = new Paint();
    private int mMaxLength = 6;
    private int mCurrentLength = 0;

    public PayPwdView(Context context) {
        super(context);
    }

    public PayPwdView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PayPwdView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mVLPaint.setAntiAlias(true);
        mVLPaint.setColor(Color.GRAY);
        mVLPaint.setStyle(Paint.Style.STROKE);
        mVLPaint.setStrokeWidth(ConvertUtils.dp2px(context, 1));

        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(Color.BLACK);
        mCirclePaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        mW = widthSize;
        mH = heightSize;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画分割线:分割线数量比密码数少1
        for (int i = 1; i < mMaxLength; i++) {
            float x = mW * i / mMaxLength;
            canvas.drawLine(x, 0, x, mH, mVLPaint);
        }
        //画密码内容
        float px, py = mH / 2;
        float halfWidth = mW / mMaxLength / 2;
        for (int i = 0; i < mCurrentLength; i++) {
            px = mW * i / mMaxLength + halfWidth;
            canvas.drawCircle(px, py, mH / 6, mCirclePaint);
        }
    }

    public void refreshStatus(int l) {
        mCurrentLength = l;
        invalidate();
    }

    void setMaxLength(int maxLength) {
        mMaxLength = maxLength;
        invalidate();
    }
}